<ui:composition template="template.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets">

	<ui:define name="head">
		<script type="text/javascript">
			//<![CDATA[
			function confirmUpload(fileType) {
				if (confirm('Are you sure you wish to upload (and overwrite) the '
						+ fileType
						+ ' file?\nUploading a corrupt file may cause system ineffeciencies or even crashes.')) {
					return true;
				}
				return false;
			}

			function showUploadJobForm(button) {
				var contentClone = $("#UploadFileTemplateDiv").clone();
				applyFileSelectEventHandler(contentClone);
				$(button).replaceWith(contentClone);
			}
			//]]>
		</script>
		<link rel="stylesheet" type="text/css"
			href="#{request.contextPath}/stylesheets/repository.css" />
	</ui:define>

	<ui:define name="content">
		<div id="RepositoryPage" class="container">
			<h:panelGroup
				rendered="#{param['configuration_upload'] == 'Success'}">
				<div class="alert alert-success">
					Configuration file <a href="repository/#{user.tenant}/conf.xml"
						target="_blank">conf.xml</a> uploaded successfully.
				</div>
			</h:panelGroup>
			
			<h:panelGroup
				rendered="#{param['job_upload'] == 'Success'}">
				<div class="alert alert-success">
					Job file <b>#{param['job_filename']}</b> uploaded successfully.
				</div>
			</h:panelGroup>
			
			<h:panelGroup rendered="#{user.configurationEditor and monitorUIPreferences.showConfXmlUploadOptions}">
				<div class="panel panel-default panel-right">
					<div class="panel-heading">Configuration file</div>
					<div class="panel-body">
						<p>
							Upload new <a href="repository/#{user.tenant}/conf.xml"
								target="_blank">conf.xml</a> file:
						</p>
						<form action="repository/#{user.tenant}/conf.xml"
							method="post" enctype="multipart/form-data">
							 
							<div class="input-group">
								<span class="input-group-btn">
									<span class="btn btn-primary btn-sm btn-file">
										Browse … <input type="file" name="file" />
									</span>
								</span>
								<input type="text" class="form-control input-sm" readonly="readonly" />
							</div>
							
							<div class="spacer-xs"></div>
							
							<button type="submit"
								class="btn btn-warning btn-block"
								onclick="return confirmUpload('configuration')">
								<span class="glyphicon glyphicon-upload" aria-hidden="true" />
								<span>Upload</span>
							</button>
						</form>
					</div>
				</div>
			</h:panelGroup>

			<h:panelGroup rendered="#{user.admin and monitorUIPreferences.showRepositoryUploadOptions}">
				<div class="panel panel-default panel-right">
					<div class="panel-heading">Repository backup + restore</div>
					<div class="panel-body">
						<p>
							You can download <a href="repository/#{user.tenant}/zip">the
								repository as a .zip file</a>. Use this as a backup or as a way to replicate a repository from one tenant to another.
						</p>
						<p>Restore repository by uploading a corresponding zip file:</p>
						<div class="spacer-sm"></div>
						
						<form action="repository/#{user.tenant}/zip" method="post"
							enctype="multipart/form-data">
							
							<div class="input-group">
								<span class="input-group-btn">
									<span class="btn btn-primary btn-file btn-sm">
										Browse … <input type="file" name="file" />
									</span>
								</span>
								<input type="text" class="form-control input-sm" readonly="readonly" />
							</div>
							
							<div class="spacer-xs"></div>
							
							<button type="submit"
								class="btn btn-warning btn-block"
								onclick="return confirmUpload('repository')">
								<span class="glyphicon glyphicon-upload" aria-hidden="true" />
								<span>Upload</span>
							</button>
						</form>
					</div>
				</div>
			</h:panelGroup>

			<h2>Jobs</h2>
			<ul class="repositoryView">
				<ui:repeat var="folder" value="#{tenantContext.jobFolder.folders}">
					<li class="folder">#{folder.name}</li>
					<ul>
						<ui:repeat var="file" value="#{folder.files}">
							<li class="file #{file.type}">#{file.name}</li>
						</ui:repeat>
					</ul>
				</ui:repeat>
				<ui:repeat var="file" value="#{tenantContext.jobFolder.files}">
					<h:panelGroup rendered="#{file.type eq 'ANALYSIS_JOB'}">
						<li class="file #{file.type}"><a
							href="repository/#{user.tenant}/jobs/#{file.name}">#{file.name}</a>
							<h:panelGroup rendered="#{user.jobEditor}"
								styleClass="uploadJobPanel">
								<form action="repository/#{user.tenant}/jobs/#{file.name}"
									method="post" enctype="multipart/form-data" class="form-inline" style="display: inline;">
										<button onclick="showUploadJobForm(this);" class="text-warning btn-textlike" type="button">
											<span class="glyphicon glyphicon-upload" aria-hidden="true" />
										</button>
								</form>
							</h:panelGroup></li>
					</h:panelGroup>
					<h:panelGroup rendered="#{file.type ne 'ANALYSIS_JOB'}">
						<li class="file #{file.type}">#{file.name}</li>
					</h:panelGroup>
				</ui:repeat>
			</ul>

			<h2>Timelines</h2>
			<ul class="repositoryView">
				<ui:repeat var="folder"
					value="#{tenantContext.timelineFolder.folders}">
					<li class="folder">#{folder.name}</li>
					<ul>
						<ui:repeat var="file" value="#{folder.files}">
							<li class="file #{file.type}"><a
								href="dashboard##{folder.name}">#{file.name}</a></li>
						</ui:repeat>
					</ul>
				</ui:repeat>
				<ui:repeat var="file" value="#{tenantContext.timelineFolder.files}">
					<li class="file #{file.type}"><a href="dashboard">#{file.name}</a></li>
				</ui:repeat>
			</ul>


			<h2>Datastores</h2>
			<ul class="repositoryView">
				<ui:repeat var="ds" value="#{jsfHelper.datastores}">
					<li class="file DATASTORE">#{ds.name}</li>
				</ui:repeat>
			</ul>

			<h2>Results</h2>
			<ul class="repositoryView">
				<ui:repeat var="folder"
					value="#{tenantContext.resultFolder.folders}">
					<li class="folder">#{folder.name}</li>
					<ul>
						<ui:repeat var="file" value="#{folder.files}">
							<li class="file #{file.type}">#{file.name}</li>
						</ui:repeat>
					</ul>
				</ui:repeat>
				<ui:repeat var="file" value="#{tenantContext.resultFolder.files}">
					<h:panelGroup rendered="#{file.type eq 'ANALYSIS_RESULT'}">
						<li class="file #{file.type}"><a
							href="repository/#{user.tenant}/results/#{file.name}">#{file.name}</a></li>
					</h:panelGroup>
					<h:panelGroup rendered="#{file.type ne 'ANALYSIS_RESULT'}">
						<li class="file #{file.type}">#{file.name}</li>
					</h:panelGroup>
				</ui:repeat>
			</ul>
		</div>
		
		<div style="visibility: hidden; display: none">
			<div id="UploadFileTemplateDiv">
				<p>Replace job file:</p>
				<div class="input-group">
					<span class="input-group-btn"> <span
						class="btn btn-primary btn-file btn-sm"> Browse … <input
							type="file" name="file" />
					</span>
					</span> <input type="text" class="form-control input-sm" readonly="readonly" />
				</div>

				<button type="submit" class="btn btn-primary btn-sm"
					onclick="return confirmUpload(\'job\')">
					<span class="glyphicon glyphicon-upload" aria-hidden="true" /> <span>Upload</span>
				</button>

			</div>
		</div>
	</ui:define>

</ui:composition>


